package com.lwx.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwx.bus.domain.Goods;
import com.lwx.bus.mapper.GoodsMapper;
import com.lwx.bus.service.GoodsService;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.vo.GoodsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lwx
 * @since 2020-04-21
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public DataGridView loadAllGoods(GoodsVo goodsVo) {
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        qw.eq(goodsVo.getProviderid()!=null,"providerid",goodsVo.getProviderid());
        qw.like(StringUtils.isNotBlank(goodsVo.getGoodsname()),"goodsname",goodsVo.getGoodsname());
        qw.like(StringUtils.isNotBlank(goodsVo.getProductcode()),"productcode",goodsVo.getProductcode());
        qw.like(StringUtils.isNotBlank(goodsVo.getPromitcode()),"promitcode",goodsVo.getPromitcode());
        qw.like(StringUtils.isNotBlank(goodsVo.getDescription()),"description",goodsVo.getDescription());
        qw.like(StringUtils.isNotBlank(goodsVo.getSize()),"size",goodsVo.getSize());
        //分页
        Page page = new Page<>(goodsVo.getPage(), goodsVo.getLimit());
        //查询数据库
        goodsMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    //增删改都要加事务
    @Transactional
    @Override
    public void deleteGoods(Integer id) {
        goodsMapper.deleteById(id);
    }

    @Transactional
    @Override
    public void batchDeleteGoods(Integer[] ids) {
        goodsMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Transactional
    @Override
    public void addGoods(Goods goods) {
        goodsMapper.insert(goods);
    }

    @Transactional
    @Override
    public void updateGoods(Goods goods) {
        goodsMapper.updateById(goods);
    }

}
